<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/view/system/common/headLogin.jsp"%>
<script type="text/javascript">

function reloadTree(){
	$('#menuTreegrid').treegrid("reload");
}

function hiddenAddForm(){	
	document.getElementById("addMenuDIV").style.display="none";
	resetFrom();
	$("#newMenu_parentId").val("");
	$("#newMenu_type").val("");
	$("#newMenu_level").val("");
}

//重置
function resetFrom() {
	//$("#updateForm").resetForm();
	$("#newMenu_name").val("");
	$("#newMenu_url").val("");
	$("#newMenu_imgurl").val("");
	$("#newMenu_manager").val("");
}

$(document).ready(function() {
	setTreeGridList();
});
function setTreeGridList(){
	$('#menuTreegrid').treegrid({
				title:'菜单权限树',
				striped:true,fit:true,
            	nowrap: false,fitColumns : true,rownumbers: true,fixed:true,animate:true,
				url:'menu/getMenuTreeList.action',
				idField:'id',
				treeField:'name'
				,columns:[[
					{field:'name',title:'名称',width:100,
		                formatter:function(value,rowData){
							if(rowData.type==1){
		                		return '<span style="color:blue">'+value+'</span>';
							}else{
		                		return '<span style="color:red">按钮:'+value+'</span>';
							}
		                }
	                },					
	                {field:'descr',title:'描述',width:60},
					{field:'url',title:'URL',width:70}
				]]
				,tools: [{iconCls: 'icon-reload',handler: function(){$("#menuTreegrid").treegrid('reload');}}]
				,onContextMenu: function(e,row){
					e.preventDefault();
					$(this).treegrid('unselectAll');
					$(this).treegrid('select', row.id);
					createColumnMenu(row);
					$('#tmenu').menu('show', {
						left: e.pageX,
						top: e.pageY
					});
				}
			});
}

var tmenu;
function createColumnMenu(rowData) {
	if (tmenu != null) {
		$("#tmenu").remove();
	}
	var menuStr = '<div id="tmenu" style="width:135px;">';
	
	if (rowData.level == 3||rowData.level == 1||rowData.level == 2) {
		menuStr += '<div data-options="iconCls:\'icon-add\'" id ="addSubMenu">添加子菜单</div>';
	}
	else if (rowData.level == 4) {
		menuStr += '<div data-options="iconCls:\'icon-add\'" id ="addButton">添加按钮</div>';
	}
	if (rowData.level != 1){
		menuStr += '<div data-options="iconCls:\'icon-remove\'" id ="remove">删除</div>';
	}
	menuStr += '</div>';
	tmenu = $(menuStr).appendTo('body');
	tmenu.menu( {
		onClick : function(item) {
			if (item.id == 'addSubMenu') {
				//addSubMenu
				showAddMenu(rowData);
				scroll(0,0);
			} else if (item.id == 'addButton') {
				//addButton
				showAddMenu(rowData);
				scroll(0,0);
			} else if (item.id == 'remove') {
				$.messager.confirm("<s:text name='mes.message'/>", "确定要删除吗??（如果包含子菜单，将一起删除!!删除后无法恢复!!）", function(r) {
					if(r){
						removeMenu(rowData);
					}
				});
			}
		}
	})	;
}
function addSubMenu(rowData){
	document.getElementById("addMenuDIV").style.display="";
	$("#newMenu_parentId").val(rowData.id);
	$("#newMenu_type").val(2);
	$("#newMenu_level").val(rowData.level+1);
	$("#menuParentLabel").text(rowData.name);
	$("#menuTypeLabel").text("菜单");
}
function addButton(rowData){
	document.getElementById("addMenuDIV").style.display="";
	$("#newMenu_parentId").val(rowData.id);
	$("#newMenu_type").val(3);
	$("#newMenu_level").val(rowData.level+1);
	$("#menuParentLabel").text(rowData.name);
	$("#menuTypeLabel").text("按钮");
}

function removeMenu(rowData){
	/* $.post("doMenuRemove.action?removeMenuId=" + rowData.id, callRemoveBack);
	if($("#newMenu_parentId").val()==rowData.id){
		hiddenAddForm();
	} */
	$.axs("menu/doMenuRemove.action", {'removeMenuId':rowData.id}, 
           function(data){
		
		if (data.status == 1) {
			$.messager.alert("<s:text name='mes.message'/>", data.msg, 'info',
					function(){
				reloadTree();
			});
			
		}else{
			$.messager.alert("提示信息", data.msg, 'error');
		}
	});
	if($("#newMenu_parentId").val()==rowData.id){
		hiddenAddForm();
	}
}


function showAddMenu(rowData){
	document.getElementById("menuAddFrame").src = "view/goMenuAdd.action?parentMenuId="+rowData.id;
}


</script>
<body>
	<table style="width: 100%; height: 100%">
		<tr>
			<td style="width: 50%; vertical-align: top;">
				<table id="menuTreegrid"></table>
			</td>
			<td style="width: 50%; vertical-align: top;">
				<iframe id="menuAddFrame" frameborder="0"
					style="width: 100%; height: 100%;" scrolling="yes">
				</iframe>
			</td>
		</tr>
	</table>
</body>
</html>
